package tgc.edu.tms.web.controller;

import java.util.HashSet;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import tgc.edu.tms.custom.AjaxResult;
import tgc.edu.tms.custom.CommonController;
import tgc.edu.tms.entity.ClassType;
import tgc.edu.tms.entity.Classroom;
import tgc.edu.tms.entity.Location;
import tgc.edu.tms.service.ClassTypeService;
import tgc.edu.tms.service.ClassroomService;
import tgc.edu.tms.service.LocationService;
import tgc.edu.tms.web.form.ClassroomForm;


@Controller
@RequestMapping(value="/dictionary/classRoom")
public class ClassroomController extends CommonController<Classroom, Integer, ClassroomForm>{

	@Autowired
	private ClassroomService classService;
	@Autowired
	private LocationService locationService;
	@Autowired
	private ClassTypeService classTypeService;
	
	@Override
	public Object save(ClassroomForm form) {
		try {
			Integer id = form.getId();
			Classroom classroom=null;
			Location location = null;
			ClassType classType = null;
			if(id!=null) {	//修改时
				classroom = classService.findById(id);
				BeanUtils.copyProperties(form, classroom,"id");
			}else {
				classroom = new Classroom();
				BeanUtils.copyProperties(form, classroom,"id","location","type");
				
			}
			location = locationService.findByName(form.getLocationName());
			classType = classTypeService.findByName(form.getTypeName());
			classroom.setLocation(location);
			classroom.setType(classType);
			classService.save(classroom);
			return new AjaxResult("数据保存成功");
		} catch (Exception e) {
			return new AjaxResult(false,"数据保存失败");
		}
	}

	@Override
	public void edit(ClassroomForm form, ModelMap map) throws InstantiationException, IllegalAccessException {
		Integer id = form.getId();
		Classroom model=new Classroom();
		model=null;
		if(id!=null) {
			model = classService.findById(id);
		}
		List<Location> list = locationService.findAll();
		List<ClassType> list2 = classTypeService.findAll();
		if (list.size()==0) {
			list=null;
		}
		if (list2.size()==0) {
			list2=null;
		}
		map.put("location", list);
		map.put("list", list2);
		map.put("model", model);
	}
	@Override
	public Specification<Classroom> buildSpec(ClassroomForm form){
		Specification<Classroom> spec = new Specification<Classroom>() {
			private static final long serialVersionUID = 1L;

			@Override
			public Predicate toPredicate(Root<Classroom> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules = new HashSet<>();
				if(StringUtils.hasText(form.getName())){
					rules.add(cb.like(root.get("location").get("name"), "%"+form.getName()+"%"));
				}
				if(StringUtils.hasText(form.getFloor())){
					rules.add(cb.like(root.get("floor"), "%"+form.getFloor()+"%"));
				}
				if(StringUtils.hasText(form.getSerialNumber())){
					rules.add(cb.like(root.get("serialNumber"), "%"+form.getSerialNumber()+"%"));
				}
				if(StringUtils.hasText(form.getTypeName())){
					rules.add(cb.like(root.get("type").get("name"), "%"+form.getTypeName()+"%"));
				}
				if(StringUtils.hasText(form.getSearch())){
					Predicate locationName = cb.like(root.get("location").get("name"), "%"+form.getSearch()+"%");
					Predicate floor = cb.like(root.get("floor"), "%"+form.getSearch()+"%");
					Predicate serialNumber = cb.like(root.get("serialNumber"), "%"+form.getSearch()+"%");
					//Predicate capacity = cb.like(root.get("capacity"), "%"+form.getSearch()+"%");
					Predicate typeName = cb.like(root.get("type").get("name"), "%"+form.getSearch()+"%"); 
					Predicate or = cb.or(locationName,floor,serialNumber,typeName);
					rules.add(or);
				}
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}
		};
		return spec;
	}
	
}
